﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using DevExpress.XtraGrid.Views.Grid;
using EFong.Contract;
using EFong.View.Proxy;

namespace EFong.View.FlightTicket
{
    public partial class Frm_FlightTicketChange : BasicForm
    {
        private Proxy.BaseServiceProxy _baseProxy {get{ return Proxy.BaseServiceProxy.GetInstance();}}
        private Proxy.BusinessServiceProxy _businessProxy {get{ return Proxy.BusinessServiceProxy.GetInstance();}}
        private Proxy.MasterServiceProxy _masterProxy { get { return Proxy.MasterServiceProxy.GetInstance(); } }

        public Frm_FlightTicketChange()
        {
            InitializeComponent();
            this.gv_FlightWay.OptionsView.NewItemRowPosition = NewItemRowPosition.Top;
            this.gv_FlightWay.OptionsBehavior.AllowDeleteRows = DevExpress.Utils.DefaultBoolean.True;
        }

        protected override void InitFields()
        {
            base.InitFields();
        }

        protected override void Events()
        {
            this.btn_OK.Click += (s, e) =>
            {
                save();
            };

            this.btn_Delete.Click += (s, e) =>
                {
                    Contract.FlightWay flightWay = this.gv_FlightWay.GetRow(this.gv_FlightWay.FocusedRowHandle) as Contract.FlightWay;

                    if (flightWay == null)
                        return;

                    flightWay.IsDelete = true;

                    this.gv_FlightWay.BeginDataUpdate();
                    this.gv_FlightWay.EndDataUpdate();
                };

            this.gv_FlightWay.CustomDrawCell += (s, e) =>
                {
                    Contract.FlightWay flightWay = this.gv_FlightWay.GetRow(e.RowHandle) as Contract.FlightWay;

                    if (flightWay == null)
                        return;

                    if (flightWay.IsDelete)
                        e.Appearance.ForeColor = Color.Maroon;
                };

            this.cmb_Vendor.EditValueChanged += (s, e) =>
            {
                Contract.RVendor vendor = this.cmb_Vendor.GetValue() as Contract.RVendor;

                if (vendor != null)
                {
                    SelVend = _masterProxy.GetVendorByID(vendor.ID).ReturnValue;
                }
                else
                {
                    SelVend = null;
                }
            };
        }

        protected override void LoadDatas()
        {
            this.cmb_OrderUser.Properties.BindingData(_baseProxy.GetEffUser().ReturnValue);
            this.bds_Vendor.BindingData(_masterProxy.GetAllFlightVendor().ReturnValue);
            this.cmb_BankAccount.Properties.BindingData(_masterProxy.GetAllEffectivedBankAccount());

            if (this.cmb_OrderUser.GetValue() == null)
                this.cmb_OrderUser.SetValue(AppParameters.LoginUser);
        }

        private void save()
        {
            UIOperManager.SaveData(
                () =>
                {
                    Contract.FlightChange detail = getDetail();
                    UpdateToDetail(detail);
                    return _businessProxy.ChangeFlightOrder(Detail, OrderDetail, Order.TotalProfit.Value).IsSuccess;
                },
                "改票成功.",
                () =>
                {
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                );
        }

        #region Detail
        private Contract.FlightChange _detail;
        public Contract.FlightChange Detail
        {
            get { return _detail; }
            set
            {
                _detail = value;
                onDetailChanged();
            }
        }

        public Contract.FlightOrder Order { get; set; }

        private Contract.FlightOrderDatail _orderDetail;
        public Contract.FlightOrderDatail OrderDetail
        {
            get { return _orderDetail; }
            set 
            {
                _orderDetail = value;
                this.bds_flightWay.BindingData(_orderDetail.FlightWayList);
            }
        }

        private void onDetailChanged()
        {
            if (Detail == null)
            {
                this.bds_flightWay.BindingData(typeof(Contract.FlightWay));
            }
            else
            {
                if (!string.IsNullOrEmpty(Detail.Ref))
                    this.txt_OrderRef.SetText(Detail.Ref);

                if (Detail.OrderUser != null)
                    this.cmb_OrderUser.SetText(Detail.OrderUser);

                this.dte_OrderDate.SetValue(Detail.OrderDate);

                this.spe_VendorPrice.SetValue(Detail.VendorPrice);

                this.spe_Price.SetValue(Detail.Price);

                txt_Remark.SetText(Detail.Remark);

                if (Detail.Vendor != null)
                {
                    RVendor vendor = new RVendor()
                    {
                        ID = Detail.Vendor.ID,
                        Name = Detail.Vendor.Name,
                    };
                    this.cmb_Vendor.SetValue(vendor);
                }
                SelVend = Detail.Vendor;

                this.cmb_PaymentType.SetDisplay(Detail.PaymentType);
                if (Detail.BankAccount != null)
                    this.cmb_BankAccount.SetValue(Detail.BankAccount);
            }
        }

        public Contract.Vendor SelVend { get; set; }

        private Contract.FlightChange getDetail()
        {
            Contract.FlightChange detail = new Contract.FlightChange();

            detail.OrderDate = dte_OrderDate.GetValue();
            detail.OrderUser = cmb_OrderUser.GetValue() as Contract.User;
            detail.Price = spe_Price.GetValue();
            detail.VendorPrice = spe_VendorPrice.GetValue();
            detail.Remark = txt_Remark.GetText();
            detail.Vendor = SelVend;
            detail.PaymentType = this.cmb_PaymentType.GetDisplay();
            detail.BankAccount = this.cmb_BankAccount.GetValue() as Contract.BankAccount;
            return detail;
        }

        private void UpdateToDetail(Contract.FlightChange detail)
        {
            Detail.OrderUser = detail.OrderUser;
            Detail.OrderDate = detail.OrderDate;
            Detail.Remark = detail.Remark;
            Detail.Price = detail.Price;
            Detail.VendorPrice = detail.VendorPrice;
            Detail.Vendor = detail.Vendor;
            Detail.BankAccount = detail.BankAccount;
            Detail.PaymentType = detail.PaymentType;
        }
        #endregion
    }
}
